import request from '@/api/request'
import { Toast } from 'antd-mobile'
import { USERINFO, USERPROFILE } from './constants'

const onchange = (res) => ({ type: USERINFO, data: res.data.data })
export const getUser = () => async (dispatch) => {
  const res = await request({
    url: '/v1_0/user',
  })
  dispatch(onchange(res))
}

export const getProfile = () => async (dispatch) => {
  const res = await request({
    url: '/v1_0/user/profile',
  })
  dispatch({ type: USERPROFILE, data: res.data.data })
}

export const editProfile = (data) => async (dispatch) => {
  await request({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data,
  })
  Toast.success('修改成功')
  dispatch(getProfile())
}

export const editPhoto = (data) => async (dispatch) => {
  await request({
    method: 'patch',
    url: '/v1_0/user/photo',
    data,
  })
  dispatch(getProfile())
}
